<?php

namespace Cuke\Models;

use Phalcon\Mvc\Model\Message;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness;
use Phalcon\Validation\Validator\InclusionIn;

class CukeGroup extends ModelBase
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $group_id;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=false)
     */
    public $user_id;

    /**
     *
     * @var string
     * @Column(type="string", length=200, nullable=false)
     */
    public $group_name;

    /**
     *
     * @var string
     * @Column(type="string", length=500, nullable=false)
     */
    public $description;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $traffic_value;

    /**
     *
     * @var string
     * @Column(type="string", length=100, nullable=false)
     */
    public $group_code;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=false)
     */
    public $status;

    /**
     *
     * @var string
     * @Column(type="string", nullable=false)
     */
    public $create_at;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=false)
     */
    public $create_id;
    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $update_at;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $update_id;
    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource("cuke_group");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'cuke_group';
    }
    public function beforeUpdate()
    {
        $this->update_at = date('Y-m-d H:i:s');
    }

    public function validation()
    {
       if(!preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}][a-zA-Z\x{4e00}-\x{9fa5}_0-9]{3,49}$/u',$this->group_name)){

           $message = new Message('分组名称格式不正确','group_name');
           $this->appendMessage($message);
           return false;
       }
       if(mb_strlen($this->description) > 200){
           $message = new Message('分组描述不能大于200字','description');
           $this->appendMessage($message);
           return false;
       }
       return true;
    }
    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return CukeGroup[]|CukeGroup|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return CukeGroup|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

}
